package com.whfc.emp.service.impl;

import com.whfc.common.result.ResultEnum;
import com.whfc.emp.dao.AppEmpWorkPlanWarnMapper;
import com.whfc.emp.dto.AppEmpWorkPlanWarnDTO;
import com.whfc.emp.entity.AppEmpWorkPlanWarn;
import com.whfc.emp.service.AppEmpWorkPlanWarnService;
import com.whfc.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工作计划报警视图服务实现类
 */
@Slf4j
@DubboService(interfaceClass = AppEmpWorkPlanWarnService.class, version = "1.0.0", timeout = 5000)
public class AppEmpWorkPlanWarnServiceImpl implements AppEmpWorkPlanWarnService {
    
    @Autowired
    private AppEmpWorkPlanWarnMapper appEmpWorkPlanWarnMapper;

    @Override
    public List<AppEmpWorkPlanWarnDTO> getAllWorkPlanWarns() throws BizException {
        try {
            log.info("查询所有工作计划报警记录");
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectAll();
            return convertToDTOList(entityList);
        } catch (Exception e) {
            log.error("查询所有工作计划报警记录失败", e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询工作计划报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getWarnsByPlanId(Integer planId) throws BizException {
        try {
            log.info("根据工作计划ID查询报警记录, planId: {}", planId);
            if (planId == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(),"工作计划ID不能为空");
            }
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectByPlanId(planId);
            return convertToDTOList(entityList);
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据工作计划ID查询报警记录失败, planId: {}", planId, e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询工作计划报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getWarnsByGuardianId(String guardianId) throws BizException {
        try {
            log.info("根据监理ID查询报警记录, guardianId: {}", guardianId);
            if (guardianId == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(),"监理ID不能为空");
            }
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectByGuardianId(guardianId);
            return convertToDTOList(entityList);
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据监理ID查询报警记录失败, guardianId: {}", guardianId, e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询监理报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getWarnsByRuleId(Integer ruleId) throws BizException {
        try {
            log.info("根据报警规则ID查询记录, ruleId: {}", ruleId);
            if (ruleId == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(),"规则ID不能为空");
            }
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectByRuleId(ruleId);
            return convertToDTOList(entityList);
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据报警规则ID查询记录失败, ruleId: {}", ruleId, e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询规则报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getWarnsByTriggerObjectId(Integer triggerObjectId) throws BizException {
        try {
            log.info("根据触发对象ID查询记录, triggerObjectId: {}", triggerObjectId);
            if (triggerObjectId == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(),"触发对象ID不能为空");
            }
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectByTriggerObjectId(triggerObjectId);
            return convertToDTOList(entityList);
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据触发对象ID查询记录失败, triggerObjectId: {}", triggerObjectId, e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询触发对象报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getCurrentValidPlanWarns() throws BizException {
        try {
            log.info("查询当前有效的计划报警记录");
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectCurrentValidPlans();
            return convertToDTOList(entityList);
        } catch (Exception e) {
            log.error("查询当前有效的计划报警记录失败", e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询当前有效计划报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getPlansWithMultipleGuardians() throws BizException {
        try {
            log.info("查询包含多个监理的工作计划报警记录");
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectPlansWithMultipleGuardians();
            return convertToDTOList(entityList);
        } catch (Exception e) {
            log.error("查询包含多个监理的工作计划报警记录失败", e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询多监理工作计划报警记录失败");
        }
    }

    @Override
    public List<AppEmpWorkPlanWarnDTO> getWarnsByRuleType(Integer ruleType) throws BizException {
        try {
            log.info("根据规则类型查询报警记录, ruleType: {}", ruleType);
            if (ruleType == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(),"规则类型不能为空");
            }
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectByRuleType(ruleType);
            return convertToDTOList(entityList);
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据规则类型查询报警记录失败, ruleType: {}", ruleType, e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"查询规则类型报警记录失败");
        }
    }

    @Override
    public Integer countWarnsByGuardianId(String guardianId) throws BizException {
        try {
            log.info("统计监理负责的报警数量, guardianId: {}", guardianId);
            if (guardianId == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(),"监理ID不能为空");
            }
            List<AppEmpWorkPlanWarn> entityList = appEmpWorkPlanWarnMapper.selectByGuardianId(guardianId);
            return CollectionUtils.isEmpty(entityList) ? 0 : entityList.size();
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            log.error("统计监理负责的报警数量失败, guardianId: {}", guardianId, e);
            throw new BizException(ResultEnum.FAILURE.getCode(),"统计监理报警数量失败");
        }
    }

    /**
     * 将实体列表转换为DTO列表
     * @param entityList 实体列表
     * @return DTO列表
     */
    private List<AppEmpWorkPlanWarnDTO> convertToDTOList(List<AppEmpWorkPlanWarn> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return null;
        }
        return entityList.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 将实体转换为DTO
     * @param entity 实体
     * @return DTO
     */
    private AppEmpWorkPlanWarnDTO convertToDTO(AppEmpWorkPlanWarn entity) {
        if (entity == null) {
            return null;
        }
        AppEmpWorkPlanWarnDTO dto = new AppEmpWorkPlanWarnDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }
}